﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Phones.Client.Repository;
using Phones.Client.Model;
using Phones.Client.ViewModel;
using System.ComponentModel.Composition;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace Phones.Client.ViewModel
{
    [Export(typeof(ServiceNodeTreeNodeViewModel))]
    public class ServiceNodeTreeNodeViewModel : TreeViewItemBaseViewModel
    {
        private ServiceNode _serviceNode;
        private readonly ObservableCollection<City> _cities = new ObservableCollection<City>();

        public ObservableCollection<City> Cities
        {
            get { return _cities; }
        }

        public ServiceNode Model
        {
            get { return _serviceNode; }
        }

        [ImportingConstructor]
        public ServiceNodeTreeNodeViewModel(ServiceNode serviceNode)
            : base(null, true)
        {
            _serviceNode = serviceNode;
            _serviceNode.Cities.ToList()
                .ForEach((x) => _cities.Add(x));
        }

        protected override void LoadChildren()
        {
            Children.Clear();

            using (var db = new OrganisationsRepository())
            {
                foreach (var zone in db.GetList(_serviceNode))
                {
                    var vm = new OrganisationTreeNodeViewModel(this, zone);
                    Children.Add(vm);
                }
            }
        }

        public void Reload(object sender)
        {
            LoadChildren();
        }
    }
}
